package org.kongqian.orgs.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.kongqian.basic.utils.AjaxResult;
import org.kongqian.basic.utils.PageList;
import org.kongqian.orgs.domain.Department;
import org.kongqian.orgs.query.DepartmentQuery;
import org.kongqian.orgs.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@Api(value = "部门管理Controller",description = "部门管理Controller")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @PutMapping
    @ApiOperation(value = "部门保存")
    public AjaxResult save(@RequestBody Department department){
        try {
            if (department.getId()!=null){
                departmentService.updateById(department);
            }else {
                departmentService.insert(department);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("操作失败");
        }
        return AjaxResult.getResult();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "部门删除")
    public AjaxResult delete(@ApiParam(required = true) @PathVariable("id") Long id){
        try {
            departmentService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("删除失败");
        }
        return AjaxResult.getResult();
    }

    @GetMapping
    @ApiOperation(value = "查询全部部门")
    public AjaxResult selectAll(){
        try {
            List<Department> departments = departmentService.selectAll();
            return AjaxResult.getResult().setObject(departments);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("查询失败");
        }
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询一个部门")
    public AjaxResult selectById(@PathVariable("id") Long id){
        try {
            Department department = departmentService.selectById(id);
            return AjaxResult.getResult().setObject(department);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("查询失败");
        }
    }

    @PostMapping
    @ApiOperation(value = "部门分页和高级查询")
    public AjaxResult listPage(@RequestBody DepartmentQuery query){
        try {
            PageList<Department> departmentPageList = departmentService.listPage(query);
            return AjaxResult.getResult().setObject(departmentPageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("查询失败");
        }
    }

    @GetMapping("/tree")
    @ApiOperation(value = "获取部门树")
    public AjaxResult getDepartmentTree(){
        try {
            List<Department> departmentTree = departmentService.getDepartmentTree();
            return AjaxResult.getResult().setObject(departmentTree);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("查询部门树失败");
        }
    }

    @PatchMapping
    @ApiOperation(value = "批量删除部门")
    public AjaxResult batchDeleteDept(@RequestBody List<Long> ids){
        try {
           departmentService.batchDeleteDept(ids);
            return AjaxResult.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getResult().setSuccess(false).setMessage("批量删除部门失败");
        }
    }
}
